<?php

include("./connect_tool.php");
    function getArrayDataFromResult($result) {
        $dataArray = array();
        while($row = $result->fetch_assoc()) {
            array_push($dataArray, $row);
        }
        return $dataArray;
    }
    function getSingleDataFromResult($result) {
        return $result->fetch_assoc();
    }
    function getFields($tableName) {
        global $conn;
        $fields = array();
        $tmpResult = $conn->query("desc `{$tableName}`;");
        while ($row = $tmpResult->fetch_assoc()) {
            array_push($fields, $row);
        }
        return $fields;
    }
    function getFieldsMap($tableName) {
        global $conn;
        $fieldsMap = array();
        $tmpResult = $conn->query("desc `{$tableName}`;");
        while ($row = $tmpResult->fetch_assoc()) {
            $fieldsMap[$row["Field"]] = $row;
        }
        return $fieldsMap;
    }
    // 获取主键
    function getKeyField($tableName) {
        global $conn;
        $keyField = "";
        $tmpResult = $conn->query("desc `{$tableName}`;");
        while ($row = $tmpResult->fetch_assoc()) {
            if($row["Key"] == "PRI") {
                $keyField = $row["Field"];
            }
        }
        return $keyField;
    }

    function getValueStr($field, $value) {
        $valueStr = $value;
        if($value === null) {
            $valueStr = "null";
        }
        else {
            if(strpos($field["Type"], "varchar") !== false) {
                $valueStr = '"' . $valueStr . '"';
            }
            else if(strpos($field["Type"], "date") !== false) {
                $valueStr = "'" . $valueStr . "'";
            }
        }
        return $valueStr;
    }

    $method = $_SERVER['REQUEST_METHOD'];
    
    if($method == "GET") {
        $tableName = $_GET["tableName"];
        $option = $_GET["option"];
        $keyField = getKeyField($tableName);
        switch($option) {
            // tableName=dept2019152038&option=delete&id=60
            case "delete":
                $id = $_GET["id"];
                $sql = "DELETE FROM " . $tableName . " WHERE " . $keyField . "= " . $id;
                $conn->query($sql);
                echo "DELETE $id OK!";
                break;
            case "get":
                // tableName=dept2019152038&option=get&type=single&id=20
                $type = $_GET["type"];
                if($type == "single") {
                    $id = $_GET["id"];
                    $sql = "SELECT * FROM " . $tableName . " WHERE " . $keyField . " = " . $id;
                    $result = $conn->query($sql);
                    $data = getSingleDataFromResult($result);
                    echo json_encode($data);
                }
                // tableName=dept2019152038&option=get&type=list
                else if($type == "list") {
                    $sql = "SELECT * FROM " . $tableName . ";";
                    $result = $conn->query($sql);
                    $detailData = getArrayDataFromResult($result);
                    $data = array();
                    $data["fields"] = getFields($tableName);
                    $data["keyField"] = $keyField;
                    $data["detailData"] = $detailData;
                    echo json_encode($data);
                }
                else {
                    echo "error";
                }
        }
    }
    else if($method == "POST") {
        $raw = file_get_contents('php://input');
        $data = json_decode($raw, true);
        // post方式
        $option = $data["option"];
        if($option == "newRecord") {
            $object = $data["object"];
            $tableName = $data["tableName"];
            $fields = getFields($tableName);
    
            $sql = "INSERT INTO `$tableName` VALUES(";
    
            // echo json_encode($fields);
            for($i = 0; $i < count($fields); $i++) {
                if($i !== 0) {
                    $sql .= ", ";
                }
                $field = $fields[$i];
                $fieldName = $field["Field"];
                $value = $object[$fieldName];
                $valueStr = getValueStr($field, $value);
                $sql .= $valueStr;
            }
            
            $sql .= ");";
            $conn->query($sql);
            echo $sql;
            echo "post ok";
        }
        else if($option == "modify") {
            $raw = file_get_contents('php://input');
            $data = json_decode($raw, true);
            // 获取参数值
            $id = $data["id"];
            $object = $data["object"];
            $tableName = $data["tableName"];
            $fieldsMap = getFieldsMap($tableName);

            $sql = "UPDATE `$tableName` SET ";
            $i = 0;
            foreach($object as $fieldName=>$valueStr) {
                if($i !== 0) {
                    $sql .= ", ";
                }
                $field = $fieldsMap[$fieldName];
                $sql .= $fieldName . " = ";
                $value = $object[$fieldName];
                $valueStr = getValueStr($field, $value);
                $sql .= $valueStr;
                $i++;
            }
            $keyField = getKeyField($tableName);
            $sql .= " WHERE `$keyField` = ". $id .";";
            $conn->query($sql);
            echo $sql;
            echo "modify ok!";
        }
    }

?>